//
//
//
//
//
//
//
//
//
//
//
//
//

import { mapGetters, mapActions } from "vuex";
import template from "./revisePageTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  data() {
    return {
      type: "",
      nickname: "",
      weixin: "",
      email: "",
      mobile: "",
      qq: "",

      names: "",

      verify: "",
      placeholder: "",
      numLength: 30,

      verifyNickname: function (value) {
        return {
          valid: /^[A-Za-z\u4e00-\u9fa5]{0,8}$/.test(value),
          msg: "只能输入汉字和字母",
        };
      },
      verifyWeixin: function (value) {
        return {
          valid: /^[a-zA-Z0-9_-]{3,30}$/.test(value),
          msg: "输入的微信格式错误！",
        };
      },
      verifyEmail: function (value) {
        return {
          valid: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(
            value
          ),
          msg: "输入的邮箱格式错误！",
        };
      },
      verifyMobile: function (value) {
        return {
          valid: /^[0-9]{11}$/.test(value),
          msg: "输入的手机号码格式错误！",
        };
      },
      verifyQQ: function (value) {
        return {
          valid: /^[1-9]{1}[0-9]{4,12}$/.test(value),
          msg: "输入的QQ号码格式错误！",
        };
      },
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    let vm = this;
    vm.type = vm.$route.query.type;
    switch (vm.type) {
      case "nickname":
        vm.verify = vm.verifyNickname;
        vm.holderName = vm.$route.query.nickname;
        vm.placeholder = "修改昵称";
        vm.names = "昵称";
        vm.numLength = 8;
        break;
      case "weixin":
        vm.verify = vm.verifyWeixin;
        vm.holderName = vm.$route.query.weixin;
        vm.placeholder = "修改微信";
        vm.names = "微信";
        break;
      case "email":
        vm.verify = vm.verifyEmail;
        vm.holderName = vm.$route.query.email;
        vm.placeholder = "修改邮箱";
        vm.names = "邮箱";
        break;
      case "mobile":
        vm.verify = vm.verifyMobile;
        vm.holderName = vm.$route.query.mobile;
        vm.placeholder = "修改手机号";
        vm.names = "手机号";
        break;
      case "qq":
        vm.verify = vm.verifyQQ;
        vm.holderName = vm.$route.query.qq;
        vm.placeholder = "修改QQ";
        vm.names = "QQ";
        vm.numLength = 13;
        break;
      default:
        break;
    }
  },
  mounted() {
    this.$nextTick(() => {
      let inp = document.querySelector("input");
      inp.focus();
    });
  },
  methods: {
    ...mapActions(["SET_USER_INFO_DATA"]),
    _reviseMsg() {
      let vm = this;
      if (vm.holderName == "" || vm.holderName == null) {
        vm.$vux.toast.text("请输入" + vm.names, "bottom");
        return;
      }
      if (vm.$route.query.nickname) {
        if (!vm.verify(vm.holderName).valid) {
          vm.$vux.toast.text("只能输入汉字和字母", "bottom");
          return;
        }
      } else if (!vm.verify(vm.holderName).valid) {
        vm.$vux.toast.text("请输入正确" + vm.names, "bottom");
        return;
      }
      let params = {};
      params.token = localStorage.getItem("token");
      params[vm.type] = vm.holderName;
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=user&a=setInfo", params)
        .then((res) => {
          if (res.status == 0) {
            let temp = vm.userInfo;
            temp[vm.type] = vm.holderName;
            vm.SET_USER_INFO_DATA({
              Obj: temp,
            });
            vm.$vux.toast.text("修改成功", "bottom");
            vm.$router.go(-1);
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              vm.$vux.toast.text(res.ret_msg, "bottom");
            }
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          console.log(err);
          vm.$vux.loading.hide();
        });
    },
  },
  watch: {},
};

// WEBPACK FOOTER //
// ./src/components/personalCenter/personal/revisePage.vue
